package com.example.livetempogara;

import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.webkit.WebView.FindListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ThreadUpdate extends AsyncTask<Void, Integer, Void>{
	Activity activity;
	long msecLast;
	long msecCurr;
	boolean stop;
	int count;
	ListView listView;
	ApplicationData appData;
	ListAdapter arrayAdapter;
	ArrayAdapter<String> adapter;
	
	public ThreadUpdate(Activity a){
		this.activity = a;
		count = 0;
		msecCurr = System.currentTimeMillis();
		appData = (ApplicationData) activity.getApplication();
	}
	
	protected void onPreExecute(){
//	    arrayAdapter = listView.getAdapter();
	}
	@SuppressLint("NewApi")
	@Override
	protected Void doInBackground(Void... params) {
		EditText refrEd = (EditText) activity.findViewById(R.id.editTextRefresh);
		int refresh = 0;
		while(true){
//			String refText = refrEd.getText().toString();
//			int refresh=0;
			if(appData.c.refresh!=0)
				refresh=appData.c.refresh;
			else
				refresh =3;
				try {
//					if(appData.c.refresh==0)
//						Thread.sleep(3000);
//					else
					Thread.sleep(refresh*1000);
				} catch (InterruptedException e1) {
//					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
//				int countRead = 0;
				try {
					appData.c.tagCounter = appData.c.getData();
				} catch (IOException e) {
//					 TODO Auto-generated catch block
					e.printStackTrace();
				}
				String[] values = appData.c.output.content.split("\n");
				for(int i = 0;i < values.length;i++){
					int start=values[i].indexOf(";")-4;
					int end = values[i].indexOf(";");
					if(start>0 && end >0)
						values[i]=values[i].substring(start,end);
					appData.c.listTags.add(values[i]);
				}
				
				count++;
//				appData.c.tagCounter=countRead;
				publishProgress(count);
//				Thread.sleep(10000);
		}
		
	}
	
	@SuppressLint("NewApi")
	protected void onProgressUpdate(Integer... progress) {
		 final ListView listViewTags = (ListView) activity.findViewById(R.id.listViewTags);

		if(count==1){
			adapter = new ArrayAdapter<String>(activity, R.layout.row, R.id.textViewList, appData.c.listTags);
			listViewTags.setAdapter(adapter);
		}
		Log.v("out", "I'm in progress");
		 for(int i =0; i<appData.c.listTags.size();i++)
				Log.v("out",appData.c.listTags.get(i));
//			ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, R.layout.row, R.id.textViewList, appData.c.listTags);
//			listViewTags.setAdapter(adapter);
		 adapter.notifyDataSetChanged();	
 	 	 Button b = (Button) activity.findViewById(R.id.buttonPausa);

		 if(appData.c.pause){

			 	b.setText("Start");
			 	int pos = listViewTags.getFirstVisiblePosition();
			 	listViewTags.setSelection(pos);
//				listViewTags.setOnScrollListener(new OnScrollListener() {
//			        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {}
//
//			        public void onScrollStateChanged(AbsListView view, int scrollState) {
//			            if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) 
//			            {
////			                if (isAutoScrolling) return;
//
////			                isAutoScrolling = true;
//			                int pos = listViewTags.getFirstVisiblePosition();
//			                listViewTags.setSelection(pos);
////			                listViewTa
////			                isAutoScrolling = false;
//			            }
//			        }
//
//			    });
				listViewTags.setTranscriptMode(ListView.TRANSCRIPT_MODE_DISABLED);

			}else{
				listViewTags.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
				b.setText("Pausa");
			}
//			listViewTags.scrollTo(0, listViewTags.getCount()-1);
			adapter.notifyDataSetChanged();
//        text.setText(new Integer(count).toString()); // Call to method in UI
    }
	
	 protected void onPostExecute(Void result) {
		 ListView listViewTags = (ListView) activity.findViewById(R.id.listViewTags);
		 for(int i =0; i<appData.c.listTags.size();i++)
				Log.v("out",appData.c.listTags.get(i));
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, R.layout.row, R.id.textViewList, appData.c.listTags);
			listViewTags.setAdapter(adapter);
//			listViewTags.scrollTo(0, listViewTags.getCount()-1);
			adapter.notifyDataSetChanged();
	    }
	
}
